﻿#coding=utf-8
#from heroes import *
import time
#from Unit import *
#from Ui import *


class BattleActive:
    """description of class"""
    g = None

    units = []

    max_time = 0
    cur_time = 0

    def __init__(self, g, units, max_time):
        self.g = g
        self.units = units
        self.max_time = max_time
        
    #def set(self, units, max_time):
        

    def show_units(self):

        self.g.ui.show_list(self.units, "")

    def battle_loop(self):

        live_units = self.units
        self.cur_time = 0
        while self.cur_time < self.max_time and len(live_units) > 1:
            
            live_units = []
            dead_units = []
            for unit in self.units:
                if unit.hp > 0:
                    live_units.append(unit)
                else:
                    dead_units.append(unit)
            
            for unit in live_units:
                if unit.hp <= 0:
                    continue
                
                if unit.can_act():
                    #self.g.ui.clear()
                    self.g.ui.show_list(live_units, "等待%s(HP:%d/%d, MP:%d/%d)动作，剩下的单位: " % (unit, unit.hp, unit.max_hp, unit.mp, unit.max_mp))
                    
                    abilities = []
                    for ability in unit.abilities:
                        if ability.can_cast():
                            abilities.append(ability)
                            
                    if len(abilities) > 0:
                        
                        self.g.ui.show_list(abilities, "%s的技能: " % unit)
                        
                        if unit.name == "ligh":
                            sel = self.g.ui.select_correct(range(0, len(abilities)), "输入技能索引:> ", "索引错误")
                        else:
                            #sel = random.randint(0, len(abilities) - 1)
                            sel = len(abilities) - 1
                        
                        unit.set_ability(abilities[sel])
                        #self.g.ui.output("\n%s选择了%s\n" % (unit.name, unit.abilities[sel].name))
                        self.g.ui.output("\n")
    
                        targets = []
                        for target in live_units:
                            if target != unit:
                                targets.append(target)
                                
                        self.g.ui.show_list(targets, "选择%s目标: " % unit.abilities[sel].name)
                        
                        if unit.name == "ligh":
                            sel = self.g.ui.select_correct(range(0, len(targets)), "输入目标索引:> ", "索引错误")
                        else:
                            sel = 0
                        self.g.ui.output("\n")
                            
                        unit.set_targets([targets[sel]])
                        #self.g.ui.output("%s选择了%s作为目标\n" % (unit.name, targets[sel].name))
    
                        unit.start_ability()
                        
                    else:
                        #self.g.ui.output("\n")
                        pass


                unit.pa_action()
            #time.sleep(self.g.time_delay)
            self.cur_time += 1

